package ysq.xyg.web.controller;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.model.po.*;
import ysq.xyg.web.model.vo.WebArticleVo;
import ysq.xyg.web.model.vo.WebBannerVo;
import ysq.xyg.web.model.vo.WebProductVo;
import ysq.xyg.web.service.*;
import ysq.xyg.web.util.WebBaseContent;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 网站phone端
 *
 * @author yu sheng qiang
 * @create 2017-03-22 11:57
 **/
@RestController
public class PhoneController {
    private static Logger log= LoggerFactory.getLogger(PhoneController.class);
    @Resource
    private WebProductService webProductService;
    @Resource
    private WebTagTypeService webTagTypeService;
    @Resource
    private WebColumnTypeService webColumnTypeService;
    @Resource
    private WebBannerService webBannerService;
    @Resource
    private WebConfigService webConfigService;
    @Resource
    private WebArticleService webArticleService;
    /**
     * 统一首页
     *
     * @param confId 系统配置id
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/phone")
    public ModelAndView index(String confId){
        WebConfig webConfig=acceptWebConfig(confId);
        ModelAndView modelAndView=new ModelAndView("/mobile/"+webConfig.getSiteStyle()+"/index");
        modelAndView.addObject("webConfig",webConfig);
        return modelAndView;
    }
    /**
     * 统一跳转自定义页面
     *
     * @param confId 系统配置id
     * @param fileName 文件名称
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/phone/page/{confId}")
    public ModelAndView page(@PathVariable String confId,String fileName){
        WebConfig webConfig=acceptWebConfig(confId);
        ModelAndView modelAndView=new ModelAndView("/mobile/"+webConfig.getSiteStyle()+"/"+fileName);
        modelAndView.addObject("webConfig",webConfig);
        return modelAndView;
    }
    /**
     * 统一跳转商品页面
     *
     * @param confId 系统配置id
     * @param productId 商品id
     * @param fileName 文件名称
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/phone/product/{confId}")
    public ModelAndView page(@PathVariable String confId,String productId,String fileName){
        WebConfig webConfig=acceptWebConfig(confId);
        ModelAndView modelAndView=new ModelAndView("/mobile/"+webConfig.getSiteStyle()+"/"+fileName);
        modelAndView.addObject("webConfig",webConfig);
        modelAndView.addObject("product",webProductService.selectByPrimaryKey(productId));
        return modelAndView;
    }
    /**
     * 统一文档页面
     *
     * @param siteSyple 网站风格
     * @param id 文章标识
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/phone/article/{siteSyple}")
    public ModelAndView articleDesc(@PathVariable String siteSyple, String id){
        ModelAndView modelAndView=new ModelAndView("/mobile/"+siteSyple+"/articleDesc");
        modelAndView.addObject("article",webArticleService.selectByPrimaryKey(id));
        return modelAndView;
    }
    /**
     * 统一关于我们页面
     *
     * @param siteSyple 网站风格
     * @param confId 系统配置id
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/phone/about/{siteSyple}")
    public ModelAndView about(@PathVariable String siteSyple, String confId){
        ModelAndView modelAndView=new ModelAndView("/mobile/"+siteSyple+"/about");
        modelAndView.addObject("webConfig",acceptWebConfig(confId));
        return modelAndView;
    }
    /**
     * 统一关于id跳转指定页面
     *
     * @param tagId 标签id
     * @param confId 系统配置id
     * @param fileName 文件名称
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/phone/tagType/{confId}")
    public ModelAndView tagType(@PathVariable String confId,String tagId,String fileName){
        WebConfig webConfig=acceptWebConfig(confId);
        String url="/mobile/"+webConfig.getSiteStyle()+"/defaultTagType";
        if(StrUtil.isNotEmpty(fileName)){
            url="/mobile/"+webConfig.getSiteStyle()+"/"+fileName;
        }
        ModelAndView modelAndView=new ModelAndView(url);
        modelAndView.addObject("webConfig",webConfig);
        WebTagType webTagType=webTagTypeService.selectByPrimaryKey(tagId);
        modelAndView.addObject("webTagType",webTagType);
        if(webTagType!=null){
            if(WebBaseContent.web_tag_type_lm.equals(webTagType.getTagType())){
                //获取栏目
                List<WebColumnType> list=webColumnTypeService.listLeftByTagId(tagId);
                if(list!=null&&list.size()>0){
                    modelAndView.addObject("colId",list.get(0).getId());
                }
                modelAndView.addObject("webColumnList",list);
            }else if(WebBaseContent.web_tag_type_wz.equals(webTagType.getTagType())){
                //获取文章
                List<WebArticle> list=webArticleService.listLeftByTagId(tagId);
                if(list!=null&&list.size()>0);{
                    modelAndView.addObject("articleId",list.get(0).getId());
                }
                modelAndView.addObject("articleList",list);
            }
        }
        return modelAndView;
    }
    /**
     * 统一关于标签id跳转指定页面
     *
     * @param colId id
     * @param confId 系统配置id
     * @param fileName 文件名称
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * * */
    @RequestMapping(value="/web/phone/column/{confId}")
    public ModelAndView column(@PathVariable String confId,String colId,String fileName){
        WebConfig webConfig=acceptWebConfig(confId);
        String url="/mobile/"+webConfig.getSiteStyle()+"/defaultTagType";
        if(StrUtil.isNotEmpty(fileName)){
            url="/mobile/"+webConfig.getSiteStyle()+"/"+fileName;
        }
        ModelAndView modelAndView=new ModelAndView(url);
        modelAndView.addObject("webConfig",webConfig);
        WebColumnType webColumnType=webColumnTypeService.selectByPrimaryKey(colId);
        modelAndView.addObject("webColumnType",webColumnType);
        modelAndView.addObject("colId",colId);
        return modelAndView;
    }
    /**
     * 统一根据标签获取所属信息
     *
     * @param tagId 标签id
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     */
    @RequestMapping(value = "/web/phone/acceptListByTagId",method = RequestMethod.POST)
    public ReturnEntity acceptListByTagId(String tagId){
        WebTagType webTagType=webTagTypeService.selectByPrimaryKey(tagId);
        if(webTagType!=null){
            if(WebBaseContent.web_tag_type_lm.equals(webTagType.getTagType())){
                //获取栏目
                return new ReturnEntity(webColumnTypeService.listLeftByTagId(tagId));
            }else  if(WebBaseContent.web_tag_type_wz.equals(webTagType.getTagType())){
                //获取文章
                return new ReturnEntity(webArticleService.listLeftByTagId(tagId));
            }else {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,"标签错误");
    }
    /**
     * 分页查询
     *
     * @param webProductVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    @RequestMapping(value = "/web/phone/webProduct/page",method = RequestMethod.POST)
    public ReturnEntity page(WebProductVo webProductVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = webProductService.page(webProductVo,pageSize,pageNum);
        return returnEntity;
    }
    /**
     * 分页查询
     *
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    @RequestMapping(value = "/web/phone/webArticle/page",method = RequestMethod.POST)
    public ReturnEntity page(WebArticleVo webArticleVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = webArticleService.page(webArticleVo,pageSize,pageNum);
        return returnEntity;
    }
    /**
     * 获取文章信息
     *
     * @param articleId 栏目id
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    @RequestMapping(value = "/web/phone/selectArticleDesc",method = RequestMethod.POST)
    public ReturnEntity selectArticleDesc(String articleId){
        return new ReturnEntity(webArticleService.selectByPrimaryKey(articleId));
    }
    /**
     * 获取栏目信息
     *
     * @param colId 栏目id
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    @RequestMapping(value = "/web/phone/selectColDesc",method = RequestMethod.POST)
    public ReturnEntity selectColDesc(String colId){
        return new ReturnEntity(webColumnTypeService.selectByPrimaryKey(colId));
    }
    /**
     * 统一根据栏目获取文档
     *
     * @param userId 账号id
     * @param colId 栏目id
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     */
    @RequestMapping(value = "/web/phone/articleList",method = RequestMethod.POST)
    public ReturnEntity acceptArticle(String userId,String colId,@RequestParam(defaultValue="-1") int num){
        List<WebArticle> webArticleList=webArticleService.acceptList(new WebArticleVo(colId,BaseConstant.base_show_type_zs,userId));
        List<WebArticle> webArticleResultList=new ArrayList<WebArticle>();
        if(num<0){
            return new ReturnEntity(webArticleList);
        }else{
            if(webArticleList!=null&&webArticleList.size()>0){
                int i=0;
                for(WebArticle webArticle:webArticleList){
                    if(i<num){
                        webArticleResultList.add(webArticle);
                    }else{
                        break;
                    }
                    i++;
                }
            }
            return new ReturnEntity(webArticleResultList);
        }

    }
    /**
     * 统一获取移动端指定栏目banner图
     *
     * @param userId 账号id
     * @param colId 栏目id
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     */
    @RequestMapping(value = "/web/phone/banner",method = RequestMethod.POST)
    public ReturnEntity acceptBanner(String userId,String colId){
        List<WebBanner> webBannerList=webBannerService.acceptList(new WebBannerVo(colId, BaseConstant.base_show_type_zs,BaseConstant.base_use_type_moblie,userId));
        return new ReturnEntity(webBannerList);
    }


    public WebConfig acceptWebConfig(String xygId){
        if(StrUtil.isEmpty(xygId)){
            xygId="20190606093629083028986917194367";
        }
        return webConfigService.selectByPrimaryKey(xygId);
    }
}
